﻿using System.Collections.Generic;
using UnityEngine;

public enum PaintType
{
    gray = 0,   //置灰模式，UISprite和UITexture通过Shader处理
    color = 1,  //颜色模式，所有Widget都简单的设置颜色
}

[DisallowMultipleComponent]
public class WigdetColor
{
    public Shader shader;               //普通shader        
    public Color color;                 //普通颜色
    public Color gradientTopColor;      //渐变上部颜色
    public Color gradientBottomColor;   //渐变下部颜色
    public Color effectColor;           //效果颜色（主要针对UILabel）

    public void InitColor(Shader shaderNor, Color colorNor, Color graTop, Color graBot, Color effect)
    {
        shader = shaderNor;
        color = colorNor;
        gradientTopColor = graTop;
        gradientBottomColor = graBot;
        effectColor = effect;
    }
}

/// <summary>
/// 设置节点（包括自己）下的所有颜色
/// </summary>
[ExecuteInEditMode]
public class WigdetPaint : MonoBehaviour
{
    //子节点（包括自己）下的所有Widget
    List<UIWidget> allWidgetList = new List<UIWidget>();
    //子节点（包括自己）下的所有Widget的颜色
    List<WigdetColor> allColorList = new List<WigdetColor>();
    List<UIPanel> allPanelList = new List<UIPanel>();

    public PaintType paintType = PaintType.gray;
    public Color paintColor = Color.white;
    public Shader grayShader;
    
    void Awake()
    {
        grayShader = Shader.Find("Custom/MaskGray2");
        allWidgetList.Clear(); allColorList.Clear();
        GetComponentsInChildren(true, allWidgetList);
        for (int i = 0; i < allWidgetList.Count; i++)
        {
            AddWidgetIdx(allWidgetList[i]);
        }
    }

    [ContextMenu("PaintAllWidgets")]
    /// <summary>
    /// 设置颜色
    /// </summary>
    public void PaintWidgets()
    {
        for (int i = 0; i < allWidgetList.Count; i++)
        {
            UIWidget widget = allWidgetList[i];
            if (widget is UIBasicSprite)
            {
                var basicSprite = widget as UIBasicSprite;
                if (PaintType.gray == paintType)
                {
                    widget.material.shader = grayShader;
                    if (!allPanelList.Contains(widget.panel))
                    {
                        allPanelList.Add(widget.panel);
                    }
                }
                else
                {
                    basicSprite.color = paintColor;
                }
            }
            else if (widget is UILabel)
            {
                UILabel label = widget as UILabel;
                label.color = paintColor;
                label.gradientTop = paintColor;
                label.gradientBottom = paintColor;
                label.effectColor = paintColor;
            }
        }
        PaintPanel();
    }

    [ContextMenu("ResetAllWidgets")]
    /// <summary>
    /// 重置颜色
    /// </summary>
    public void ResetWidgets()
    {
        for (int i = 0; i < allWidgetList.Count; i++)
        {
            UIWidget widget = allWidgetList[i];
            WigdetColor widgetColor = allColorList[i];
            if (widget is UIBasicSprite)
            {
                var basicSprite = widget as UIBasicSprite;
                if (PaintType.gray == paintType)
                {
                    widget.material.shader = widgetColor.shader;
                    if (!allPanelList.Contains(widget.panel))
                    {
                        allPanelList.Add(widget.panel);
                    }
                }
                else
                {
                    basicSprite.color = widgetColor.color;
                }
            }
            else if (widget is UILabel)
            {
                UILabel label = widget as UILabel;
                label.color = widgetColor.color;
                label.gradientTop = widgetColor.gradientTopColor;
                label.gradientBottom = widgetColor.gradientBottomColor;
                label.effectColor = widgetColor.effectColor;
            }
        }
        PaintPanel();
    }

    public void PaintPanel()
    {
        for (int i = 0; i < allPanelList.Count; i++)
        {
            allPanelList[i].enabled = false;
            allPanelList[i].enabled = true;
        }
        allPanelList.Clear();
    }

    public void SetGray(UIWidget widget, bool isGray)
    {
        if (isGray)
        {
            widget.material.shader = grayShader;
        }
        else
        {
            int idx = AddWidgetIdx(widget);
            widget.material.shader = allColorList[idx].shader;
        }
        if (!allPanelList.Contains(widget.panel))
        {
            allPanelList.Add(widget.panel);
        }
    }

    public int AddWidgetIdx(UIWidget widget)
    {
        if (!allWidgetList.Contains(widget))
        {
            WigdetColor color = new WigdetColor();
            if (widget is UILabel)
            {
                UILabel label = widget as UILabel;
                color.InitColor(label.material.shader, label.color, label.gradientTop,
                    label.gradientBottom, label.effectColor);
            }
            else if (widget is UIBasicSprite)
            {
                UIBasicSprite basicSprite = widget as UISprite;
                color.InitColor(basicSprite.material.shader, basicSprite.color,
                    paintColor, paintColor, paintColor);
            }
            allWidgetList.Add(widget);
            allColorList.Add(color);
            return allWidgetList.Count;
        }
        return allWidgetList.IndexOf(widget);
    }
}